<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/22
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 积分
 * @Auth(name="token",nologin={"no"})
 * @Controller("/user.score/")
 */
class Score extends UserBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];


    /**
     * 获取统计积分
     * @Action
     * @return void
     */
    public function get_count()
    {
        $where['user_id'] = $this->auth->user()->id;
        //快到期  status状态:1=待使用,3=已使用,4=已过期
        $due_soon=  Db::name('user_score')->whereEx($where)->where('status', '=', '1')->where('expire_time', '<', time() + 86400 * 30)->sum('score');
        $sum = Db::name('user_score')->whereEx($where)->sum('score');
        if(!$due_soon)
            $due_soon = 0;
        $data['due_soon'] = $due_soon;

        if(!$sum)
            $sum = 0;
        $data['sum'] = $sum;



        // $data['this_month_inc'] = Db::name('user_score')->whereEx($where)->whereTime('createtime', 'm')->where('status','2')->sum('signlog');  
        // $data['this_month_dec'] = Db::name('user_score')->whereEx($where)->whereTime('createtime', 'm')->where('status','1')->sum('signlog');  
        return $this->success('统计', $data);
    }


    ///`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    //   `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '会员ID',
    //   `score` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '现有积分',
    //   `status` enum('1','2','3','4') COLLATE utf8mb4_unicode_ci DEFAULT '1' COMMENT '状态:1=待使用,3=已使用,4=已过期',
    //   `sum_score` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '总积分',
    //   `log` text COLLATE utf8mb4_unicode_ci COMMENT '使用记录',
    //   `createtime` bigint(16) unsigned DEFAULT NULL COMMENT '创建时间',
    //   `expire_time` bigint(16) unsigned DEFAULT NULL COMMENT '过期时间',


    /**
     * 获取我的积分记录列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $date 月
     * @param string $status 状态:1=待使用,3=已使用,4=已过期
     * @return void
     * @Action
     */
    public function get_score_list(?string $date = null, ?int $page = 1, ?int $limit = 10, ?string $status = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $data = [];
        $data['user_id'] = $this->auth->user()->id;

        if ($date) {
            list($s, $e) = $this->GetMonthTime($date);
            $data['createtime'] = ['>=', $s];
            $data['and'] = ['createtime' => ['<=', $e]];
        }
        $status1 = Db::name('user_score')->whereEx($data)->where('status', '=', '1')->sum('score');
        $status3 = Db::name('user_score')->whereEx($data)->where('status', '=', '3')->sum('score');
        $status4 = Db::name('user_score')->whereEx($data)->where('status', '=', '4')->sum('score');
        if (!$status1)
            $status1 = 0;
        if (!$status3)
            $status3 = 0;
        if (!$status4)
            $status4 = 0;
        if ($status !== null && $status !== '')
            $data['status'] = $status;

        $list = Db::name('user_score')->whereEx($data)->orderRaw('id desc')->page($page, $limit)->select()->getArray();
        foreach ($list as &$val) {
            $val['log'] = json_decode($val['log'],true);
            $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        }
        $this->success('ok', ['status1' => $status1, 'status3' => $status3,'status4' => $status4, 'list' => $list]);
    }




    // /**
    //  * 获取签到记录列表📸
    //  *
    //  * @param string $date 年月(可不传
    //  * @param integer $page 1
    //  * @param integer $limit 10
    //  * @return void
    //  * @Action
    // */
    // public function get_signlog_list(?int $page = 1,?int  $limit = 10, $date = null)
    // {
    //     $page = $page?:1;
    //     $limit = $limit?:10;
    //     if ($date) {
    //         $date = date('Y-m', strtotime($date));
    //         $e_date = date('Y-m-d 23:59:59', strtotime($date . " +1 month -1 day"));
    //         $data['createtime'] = ['between time', [$date, $e_date]];
    //     }
    //     $data['user_id'] = $this->auth->user()->id;
    //     $list = Db::name('signlog')->whereEx($data)->orderRaw('createtime desc')->page($page, $limit)->select()->getArray();
    //     if ($list)
    //         foreach ($list as $key => &$val) {
    //             $this->ChangeFieldData($val);
    //         }
    //     return $this->success('ok', $list);
    // }

    // /**
    //  * 获取签到记录详情📸
    //  *
    //  * @param int $id 签到id
    //  * @return void
    //  * @Action
    // */
    // public function get_signlog_data($id = null)
    // {

    //     $data['id'] = $id;
    //     $data['user_id'] = $this->auth->user()->id;
    //     $res = Db::name('signlog')->whereEx($data)->find();
    //     if (!$res) {
    //         return $this->error('没有该信息');
    //     }
    //     $this->ChangeFieldData($res);
    //     return $this->success('ok', $res);
    // }







    // /**
    //  * 改变字段
    //  *
    //  * @param array $val 
    //  * @return void
    //  */
    // protected function ChangeFieldData(&$val)
    // {
    //     $val['date_str'] = $this->TimeToStr($val['createtime']);
    //     $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
    //     return $val;
    // }
}
